package com.ewzj.bigdata.mvp.ui.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.InputFilter
import android.text.TextUtils
import com.ewzj.bigdata.R
import com.ewzj.bigdata.app.config.AppKey
import com.ewzj.bigdata.di.component.DaggerNormalComponent
import com.ewzj.bigdata.di.module.NormalModule
import com.ewzj.bigdata.mvp.contract.NormalContract
import com.ewzj.bigdata.mvp.presenter.NormalPresenter
import com.jakewharton.rxbinding2.view.RxView
import com.jess.arms.base.BaseActivity
import com.jess.arms.di.component.AppComponent
import kotlinx.android.synthetic.main.activity_edit.*

class EditActivity : BaseActivity<NormalPresenter>(), NormalContract.View {
    val str by lazy { intent.getStringExtra("str") }
    val issign by lazy { intent.getBooleanExtra("issign", false) }
    val title by lazy { intent.getStringExtra("title") }
    override fun initData(savedInstanceState: Bundle?) {
        initShow()
    }

    override fun initView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_edit
    }

    override fun setupActivityComponent(appComponent: AppComponent?) {
        DaggerNormalComponent.builder().appComponent(appComponent)
                .normalModule(NormalModule(this))
                .build().inject(this)
    }

    override fun showLoading() {
    }

    override fun launchActivity(intent: Intent?) {
    }

    override fun hideLoading() {
    }

    override fun killMyself() {
    }

    override fun showMessage(message: String?) {
    }

    fun initShow() {
        if (!TextUtils.isEmpty(title)) {
            tv_title.setText(title)
        }
        if (issign) {
            tv_title.setText("个性签名")
            edit_input.setHint("个性签名不能超过30个字哟～")
            edit_input.setFilters(arrayOf<InputFilter>(InputFilter.LengthFilter(30)))
        } else {
            tv_title.setText("昵称")
            edit_input.setHint("昵称不能超过10个字哟～")
            edit_input.setFilters(arrayOf<InputFilter>(InputFilter.LengthFilter(10)))
        }
//        tvTitle.setText("编辑");
        RxView.clicks(iv_back).subscribe({ activityresult() })
        RxView.clicks(tv_save).subscribe({ activityresult() })
        edit_input.setText(str)
    }

    companion object {
        fun startEditActivity(context: Activity, str: String, issign: Boolean, title: String) {
            val intent = Intent(context, EditActivity::class.java)
            intent.putExtra("str", str)
            intent.putExtra("issign", issign)
            intent.putExtra("title", title)
            if (issign) {
                context.startActivityForResult(intent, AppKey.REQUEST_SIGNCODE)
            } else {
                context.startActivityForResult(intent, AppKey.REQUEST_CODE)
            }

        }
    }

    fun activityresult() {
        var bundle = Bundle()
        bundle.putString(AppKey.EXTRA_DATA, edit_input.text.toString())
        intent.putExtras(bundle)
        setResult(Activity.RESULT_OK, intent)
        finish()
    }
}
